package exception;

import java.util.Scanner;

public class ReverseArray4 {
    //交换数组中的元素a【idx1】和a[idx2]
    public static void swap(int[] a,int idx1,int idx2){
        int t=a[idx1];
        a[idx1]=a[idx2];
        a[idx2]=t;
    }

    //对数组a的元素进行倒序排列
    public static void reverse(int[] a){

        try{
        for (int i = 0; i <a.length/2 ; i++) {
            swap(a,i,a.length-i);
        }
        }catch (ArrayIndexOutOfBoundsException e){//当接收到ArrayIndexOutofBoundsException异常时
            throw new RuntimeException("reverse的BUG？",e);//处理方法时新创建并抛出新异常的处理
        }

    }


    public static void main(String[] args) {
        Scanner std=new Scanner(System.in);
        System.out.println("元素的个数：");
        int n=std.nextInt();
        int[] x=new int[n];
        for (int i = 0; i <n ; i++) {
            System.out.println("x["+i+"]:");
            x[i]=std.nextInt();
        }

        try{reverse(x);
            System.out.println("数组倒序完毕");
            for (int i = 0; i < n; i++) {
                System.out.println("x["+i+"]="+x[i]);
            }}catch (RuntimeException e){
            System.out.println("异常："+e);
            System.out.println("异常原因:"+e.getCause());
        }

    }




}
